package com.situ.student.controller;

import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.student.entity.PageBean;
import com.situ.student.entity.Student;
import com.situ.student.service.IStudentService;
import com.situ.student.service.impl.StudentServiceImpl;

public class StudentServlet extends HttpServlet {
	private IStudentService studentService = new StudentServiceImpl();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println(req.getContextPath());//打印项目名 Java1807Web
		System.out.println(req.getServletPath());// student
		System.out.println(req.getRequestURI());// Java1807Web/student
		System.out.println(req.getRequestURI());
		
		req.setCharacterEncoding("utf-8");//解决post请求乱码问题，放在最上面
		String method = req.getParameter("method");//传过来的方法
		switch (method) {
		//展示
		case "list":
			list(req, resp);
			break;
		//根据id删除	
		case "deleteById":
			deleteById(req, resp);
			break;
		//批量删除
		case "deleteAll":
			deleteAll(req, resp);
			break;	
		
		//添加	
		case "insert":
			insert(req, resp);
			break;
		//修改	
		case "toUpdate":
			toUpdate(req,resp);
			break;
			
		case "update":
			update(req,resp);
			break;
		//分页
		case "pageList":
			pageList(req, resp);
			break;
			
		default:
			break;
		}
	}
	private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String[] selectIds = req.getParameterValues("selectIds");
		System.out.println(Arrays.toString(selectIds));
		studentService.deleteAll(selectIds);
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		
	}
	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//接受请求参数，封装成对象
		String pageNoStr = req.getParameter("pageNo");
		if (pageNoStr == null || pageNoStr.equals("")) {
			pageNoStr = "1";
		}
		int pageNo = Integer.parseInt(pageNoStr);
		
		String pageSizeStr = req.getParameter("pageSize");
		if (pageSizeStr == null || pageSizeStr.equals("")) {
			pageSizeStr = "3";
		}
		int pageSize = Integer.parseInt(pageSizeStr);
		//封装成pageBean，调用业务逻辑
		PageBean pageBean =  studentService.getPageBean(pageNo,pageSize);
		System.out.println(pageBean);
		//控制界面跳转
		req.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("student_list.jsp").forward(req, resp);
	}
	
	
	//真正修改
	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//1、得到浏览器输入的参数信息
				Integer id = Integer.parseInt(req.getParameter("id"));
				String name = req.getParameter("name");
				Integer age = Integer.parseInt(req.getParameter("age"));
				String gender = req.getParameter("gender");
				Student student = new Student(id, name, age, gender);
				//2、调用sevice
				boolean result = studentService.update(student);
				System.out.println(result ? "成功" : "失败");
				//3、重定向
				resp.sendRedirect(req.getContextPath()+"/student?method=pageList");//防止项目名写死
	}

	//欲修改
	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//根据ID查出要修改的学生对象
		Integer id = Integer.parseInt(req.getParameter("id"));
		Student student =  studentService.findById(id);//根据查找指定学生对象，直接使用
		//转发到student_update.jsp页面
		req.setAttribute("student", student);
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp); 
	}
	//添加
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//1、得到浏览器输入的参数信息
		String name = req.getParameter("name");
		Integer age = Integer.parseInt(req.getParameter("age"));
		String gender = req.getParameter("gender");
		Student student = new Student(name, age, gender);
		//2、调用sevice
		boolean result = studentService.insert(student);
		System.out.println(result ? "成功" : "失败");
		//3、重定向
//		resp.sendRedirect(req.getContextPath() +"/student?method=list");//原list
		resp.sendRedirect(req.getContextPath() +"/student?method=pageList");
	}

	//删除根据ID
	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//1、得到浏览器传递过来的参数信息
		//拿到int类型的id
		Integer id = Integer.parseInt(req.getParameter("id"));
		//2、调用service处理
		// deleteById调用了service中的deleteById
		boolean result = studentService.deleteById(id);
		System.out.println(result ? "成功" : "失败");
		//3、重定向到列表界面
		resp.sendRedirect(req.getContextPath() +"/student?method=pageList");
		
		//转发
		//req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}
	
   //展示
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//List<Student> list = studentService.list();//service方法得到列表
		
		String name = req.getParameter("name");
		//调用service
		List<Student> list = new ArrayList<>();
		if (name == null || name.equals("")) {//查找所有
		list = studentService.list();
			} else {
		
		//模糊查找
		list = studentService.findByName(name);
		list = new ArrayList<>();
		Student student = new Student(22, name + " Java1807", 30, "男");
		list.add(student);
		}
		req.setAttribute("list", list);
		
		
		
		//展示
		for (Student student : list) {  //遍历
			System.out.println(student);
		}
		//list放到req里面
		req.setAttribute("list", list);
		//转发页面展示
		req.getRequestDispatcher("student_list.jsp").forward(req, resp);
			
	}
}	
